package operate

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	"hft/Http/Services/operate"
	"hft/Http/Services/operate/operate_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type Operate struct{}

func (*Operate) Add(c *gin.Context) {
	var data *operate_model.OperateAccount
	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidInfo, c)
		return
	}

	err = operate.OperateService.Add(data, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*Operate) Edit(c *gin.Context) {
	var data *operate_model.OperateAccount
	c.ShouldBindJSON(&data)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidInfo, c)
		return
	}
	err = operate.OperateService.Edit(data, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*Operate) ChangeStatus(c *gin.Context) {
	var account *admin.Account
	c.ShouldBindJSON(&account)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidInfo, c)
		return
	}
	err = operate.OperateService.ChangeStatus(account, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*Operate) List(c *gin.Context) {
	var paginate utils.Page
	c.ShouldBind(&paginate)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidInfo, c)
		return
	}
	list, err := operate.OperateService.List(&paginate, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(list, c)
}

func (*Operate) Delete(c *gin.Context) {
	var ids *request.IDS
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidInfo, c)
		return
	}
	err = operate.OperateService.Delete(ids.IDS, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

var OperateController = new(Operate)
